<?php
class omeanalysts_ome_shop extends ectools_analysis_abstract implements ectools_analysis_interface{
    public $report_type = 'true';
    public $type_options = array(
        'display' => 'true',
    );
    public $logs_options = array(
        '1' => array(   
            'name' => '新增订单量',
            'flag' => array(),
            'memo' => '订单数量',
            'icon' => 'money.gif',
        ),
        '2' => array(
            'name' => '付款订单量',
            'flag' => array(),
            'memo' => '已付款订单数量',
            'icon' => 'money_delete.gif',
        ),
        '3' => array(
            'name' => '发货订单量',
            'flag' => array(),
            'memo' => '已发货订单数量',
            'icon' => 'coins.gif',
        ),
        '4' => array(
            'name' => '退换货订单量',
            'flag' => array(),
            'memo' => '退换货的订单数量',
            'icon' => 'coins.gif',
        ),
        '5' => array(
            'name' => '订单销售额',
            'flag' => array(),
            'memo' => '客户实际应付的订单金额',
            'icon' => 'coins.gif',
        ),
        '6' => array(
            'name' => '订单退货率',
            'flag' => array(),
            'memo' => '退换货的订单数量占所有订单量的比例',
            'icon' => 'coins.gif',
        ),
    );
    public $graph_options = array(
        'hidden' => false,
        'iframe_height' => 180,
        'callback' => 'omeanalysts_ome_graph_month',
    );

    public function __construct(&$app){
        parent::__construct($app);
        $typeObj = $this->app->model('ome_type');
        $flagList = $typeObj->get_shop();
        foreach($this->logs_options as $key=>$val){
            $this->logs_options[$key]['flag'][0] = '全部';
            foreach($flagList as $shop){
                $this->logs_options[$key]['flag'][$shop['relate_id']] = $shop['name'];
            }
        }
    }

    public function get_type(){
        $lab = '店铺';
        $typeObj = $this->app->model('ome_type');
        $data = $typeObj->get_shop();
        $return = array(
            'lab' => $lab,
            'data' => $data,
        );
        return $return;
    }

    public function get_logs($time){
        $filter = $this->_params;
        $filter = array(
            'time_from' => date('Y-m-d',$time),
            'time_to' => date('Y-m-d',$time),
            'order_status' => $filter['order_status'],
        );
        $filterPay = array(
            'time_from' => $filter['time_from'],
            'time_to' => $filter['time_to'],
            'pay_status' => 1,
        );
        $filterDelivery = array(
            'time_from' => $filter['time_from'],
            'time_to' => $filter['time_to'],
            'ship_status' => 1,
        );

        $shopObj = $this->app->model('ome_shop');

        $newOrder = $shopObj->get_order($filter);
        $payOrder = $shopObj->get_order($filterPay);
        $deliveryOrder = $shopObj->get_order($filterDelivery);
        $reshipOrder = $shopObj->get_reship($filter);
        $reship_ratio = $newOrder['num']?number_format($reshipOrder['num']/$newOrder['num'],2):0;

        $result[] = array('type'=>0, 'target'=>1, 'flag'=>0, 'value'=>$newOrder['num']);
        $result[] = array('type'=>0, 'target'=>2, 'flag'=>0, 'value'=>$payOrder['num']);
        $result[] = array('type'=>0, 'target'=>3, 'flag'=>0, 'value'=>$deliveryOrder['num']);
        $result[] = array('type'=>0, 'target'=>4, 'flag'=>0, 'value'=>$reshipOrder['num']);
        $result[] = array('type'=>0, 'target'=>5, 'flag'=>0, 'value'=>number_format($newOrder['amount'],2,".",""));
        $result[] = array('type'=>0, 'target'=>6, 'flag'=>0, 'value'=>$reship_ratio);

        $typeObj = $this->app->model('ome_type');
        $flagList = $typeObj->get_shop();
        foreach($flagList as $key=>$val){
            $shop_filter = array(
                'time_from' => date('Y-m-d',$time),
                'time_to' => date('Y-m-d',$time),
                'type_id' => $val['type_id'],
                'order_status' => $filter['order_status'],
            );
            $shop_filterPay = array(
                'time_from' => $filter['time_from'],
                'time_to' => $filter['time_to'],
                'type_id' => $val['type_id'],
                'pay_status' => 1,
            );
            $shop_filterDelivery = array(
                'time_from' => $filter['time_from'],
                'time_to' => $filter['time_to'],
                'type_id' => $val['type_id'],
                'ship_status' => 1,
            );
            $shop_newOrder = $shopObj->get_order($shop_filter);
            $shop_payOrder = $shopObj->get_order($shop_filterPay);
            $shop_deliveryOrder = $shopObj->get_order($shop_filterDelivery);
            $shop_reshipOrder = $shopObj->get_reship($shop_filter);
            $shop_reship_ratio = $shop_newOrder['num']?number_format($shop_reshipOrder['num']/$shop_newOrder['num'],2):0;

            $result[] = array('type'=>0, 'target'=>1, 'flag'=>$val['relate_id'], 'value'=>$shop_newOrder['num']);
            $result[] = array('type'=>0, 'target'=>2, 'flag'=>$val['relate_id'], 'value'=>$shop_payOrder['num']);
            $result[] = array('type'=>0, 'target'=>3, 'flag'=>$val['relate_id'], 'value'=>$shop_deliveryOrder['num']);
            $result[] = array('type'=>0, 'target'=>4, 'flag'=>$val['relate_id'], 'value'=>$shop_reshipOrder['num']);
            $result[] = array('type'=>0, 'target'=>5, 'flag'=>$val['relate_id'], 'value'=>number_format($shop_newOrder['amount'],2,".",""));
            $result[] = array('type'=>0, 'target'=>6, 'flag'=>$val['relate_id'], 'value'=>$shop_reship_ratio);
        }

        return $result;
    }

    public function ext_detail(&$detail){
        $filter = $this->_params;
        if(isset($filter['report']) && $filter['report']=='month'){
            $time_from = strtotime($filter['time_from']);
            $time_to = explode('-',$filter['time_to']);
            $filter['time_from'] = date("Y-m",$time_from).'-01';
            $filter['time_to'] = date("Y-m-d",mktime(0, 0, 0, $time_to[1]+1, 0, $time_to[0]));
        }

        $shopObj = $this->app->model('ome_shop');
        $newOrder = $shopObj->get_order($filter);
        $reshipOrder = $shopObj->get_reship($filter);
        $reship_ratio = $newOrder['num']?number_format($reshipOrder['num']/$newOrder['num'],2):0;

        $detail['订单销售额']['value'] = $detail['订单销售额']['value']?number_format($detail['订单销售额']['value'],2,"."," "):0;
        $detail['订单退货率']['value'] = $reship_ratio;
    }

    public function finder(){
        return array(
            'model' => 'omeanalysts_mdl_ome_shop',
            'params' => array(
                'actions'=>array(
                    array(
                        'label'=>app::get('omeanalysts')->_('生成报表'),
                        'class'=>'export',
                        'icon'=>'add.gif',
                        'href'=>'index.php?app=omeanalysts&ctl=ome_analysis&act=shop&action=export',
                        'target'=>'{width:400,height:170,title:\'生成报表\'}'),
                ),
                'title'=>app::get('omeanalysts')->_('店铺销售情况'),
                'use_buildin_recycle'=>false,
                'use_buildin_selectrow'=>false,
            ),
        );
    }
}